{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  [0 1 2 3 4]\n",
      "a[1]:  1\n",
      "a[-1]:  4\n",
      "a[[1, 3]]:  [1 3]\n",
      "b: \n",
      " [[0 1 2 3]\n",
      " [4 5 6 7]]\n",
      "b[0]:  [0 1 2 3]\n",
      "b[-1]:  [4 5 6 7]\n",
      "b[0][2]:  2\n",
      "b[0, 2]:  2\n",
      "b[0][[1, 2, 3]]:  [1 2 3]\n",
      "b[[0, 1], 1]:  [1 5]\n",
      "b[[0, 1], [0, 2]]:  [0 6]\n"
     ]
    }
   ],
   "source": [
    "# 一维数组\n",
    "a = np.arange(0, 5)\n",
    "print(\"a: \", a)\n",
    "\n",
    "# 以索引访问一维数组\n",
    "print(\"a[1]: \", a[1])\n",
    "print(\"a[-1]: \", a[-1])\n",
    "print(\"a[[1, 3]]: \", a[[1, 3]])\n",
    "\n",
    "# 二维数组\n",
    "b = np.arange(0, 8).reshape(2, 4)\n",
    "print(\"b: \\n\", b)\n",
    "\n",
    "# 以索引的机制访问二维数组\n",
    "print(\"b[0]: \", b[0])    # 访问第一列数据\n",
    "print(\"b[-1]: \", b[-1])  # 访问最后一列数据\n",
    "print(\"b[0][2]: \", b[0][2]) # 访问二维数组中某个位置的数据\n",
    "print(\"b[0, 2]: \", b[0, 2]) # 访问二维数组中某个位置的数据\n",
    "print(\"b[0][[1, 2, 3]]: \", b[0][[1, 2, 3]])     # 访问二维数组中某行多个数据\n",
    "print(\"b[[0, 1], 1]: \", b[[0, 1], 1])           #访问二维数组中某列多个数据\n",
    "print(\"b[[0, 1], [0, 2]]: \", b[[0, 1], [0, 2]]) # 范围二维数组中多个点的数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a[0:15:3]:  [ 0  3  6  9 12]\n",
      "\n",
      "b: \n",
      " [[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]]\n",
      "b[1:2, 1:4]: \n",
      " [[6 7 8]]\n",
      "b[[0, 2], 1:4]: \n",
      " [[ 1  2  3]\n",
      " [11 12 13]]\n"
     ]
    }
   ],
   "source": [
    "# 产生一维数组\n",
    "a = np.arange(0, 15)\n",
    "\n",
    "# 切片访问一维数组\n",
    "print(\"a[0:15:3]: \", a[0:15:3])\n",
    "\n",
    "# 产生二维数组\n",
    "b = np.arange(0, 15).reshape(3, 5)\n",
    "print(\"\\nb: \\n\", b)\n",
    "\n",
    "# 以切片方式访问二维数组\n",
    "print(\"b[1:2, 1:4]: \\n\", b[1:2, 1:4])\n",
    "print(\"b[[0, 2], 1:4]: \\n\", b[[0, 2], 1:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.迭代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a: \n",
      " [[  0.   1.   2.   3.   4.]\n",
      " [  5.   6.   7.   8.   9.]\n",
      " [ 10.  11.  12.  13.  14.]]\n",
      "0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 \n",
      "\n",
      "0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 \n",
      "\n",
      "[ 0.  1.  2.  3.  4.]\n",
      "[ 5.  6.  7.  8.  9.]\n",
      "[ 10.  11.  12.  13.  14.]\n",
      "求每一行的平均值:  [ 5.  6.  7.  8.  9.]\n",
      "求每一列的平均值:  [  2.   7.  12.]\n",
      "求每一行的平均值:  [ 5.  6.  7.  8.  9.]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(0, 15, dtype=np.float32).reshape(3, 5)\n",
    "print(\"a: \\n\", a)\n",
    "\n",
    "# 迭代访问每一个元素\n",
    "for i in range(3):\n",
    "    for j in range(5):\n",
    "        print(a[i, j], end=\" \")\n",
    "\n",
    "# 通过flat访问每一个元素\n",
    "print(\"\\n\")\n",
    "for i in a.flat:\n",
    "    print(i, end=\" \")\n",
    "    \n",
    "# 访问每一行数据\n",
    "print(\"\\n\")\n",
    "for row in a:\n",
    "    print(row)\n",
    "    \n",
    "# 使用apply_along_axis()处理一行或一列数据\n",
    "print(\"求每一行的平均值: \", np.apply_along_axis(np.mean, axis=0, arr=a))\n",
    "print(\"求每一列的平均值: \", np.apply_along_axis(np.mean, axis=1, arr=a))\n",
    "\n",
    "def min(a):\n",
    "    return np.mean(a)\n",
    "\n",
    "print(\"求每一行的平均值: \", np.apply_along_axis(min, axis=0, arr=a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
